package com.jkzx;

import com.jkzx.NoveltyActivity.TabFragmentPagerAdapter;
import com.jkzx.view.SyncHorizontalScrollView;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.ListFragment;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.LinearInterpolator;
import android.view.animation.TranslateAnimation;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class SampleListFragment extends  android.support.v4.app.Fragment {
	private int currentIndicatorLeft = 0;
	public static final String ARGUMENTS_NAME = "arg";
	public static String[][] tabTitles = {{ "卫生法律", "行政法规", "部门规章"},{ "传染疾病", "环境卫生", "辐射卫生", "职业卫生"},{ "直属单位", "传染病医院", "疫苗注射网点"},{ "亚洲", "欧洲", "非洲", "南美洲", "北美洲", "大洋洲"},{ "妇幼保健", "疫苗接种", "营养与食品", "健康促进"}};	//标题, "选项4", "选项5"
	public String[] titles={"政策法规","疾控知识","预防机构","旅行健康","生活信息"};
	public static int[] tabId = {1,2,3,4};	//标题, "选项4", "选项5"  
	private ViewPager mViewPager;
	private RadioGroup rg_nav_content;
	private ImageView iv_nav_indicator;
	private SyncHorizontalScrollView mHsv;
	private LayoutInflater mInflater;
	private int indicatorWidth;
	private RelativeLayout rl_nav;
	private ImageView iv_nav_left;
	private ImageView iv_nav_right;
	private TabFragmentPagerAdapter mAdapter;
	private int category=3;
	private TextView tv_headTitle;

	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mView = inflater.inflate(R.layout.newmessage, null);
		mViewPager = (ViewPager) mView.findViewById(R.id.mViewPager);
		rg_nav_content = (RadioGroup)mView.findViewById(R.id.rg_nav_content);
		tv_headTitle = (TextView)mView.findViewById(R.id.tv_headTitle);
		iv_nav_indicator = (ImageView)mView.findViewById(R.id.iv_nav_indicator);
		mHsv = (SyncHorizontalScrollView)mView.findViewById(R.id.mHsv);
		rl_nav = (RelativeLayout)mView.findViewById(R.id.rl_nav);
//		category=4;
		initView();
		setListener();
		tv_headTitle.setText(titles[category]);
		return mView;
	}
	private void initView() {
		//获取分辨�?		
		DisplayMetrics dm = new DisplayMetrics();
		getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
		
		indicatorWidth = dm.widthPixels / 4;
		
		LayoutParams cursor_Params = iv_nav_indicator.getLayoutParams();
		cursor_Params.width = indicatorWidth;// 初始化滑动下标的 ?		
		iv_nav_indicator.setLayoutParams(cursor_Params);
		
		mHsv.setSomeParam(rl_nav, iv_nav_left, iv_nav_right, getActivity());
		
		//获取布局填充�?		
		mInflater = (LayoutInflater)getActivity().getSystemService(getActivity().LAYOUT_INFLATER_SERVICE);

		//另一种方式获�?//		LayoutInflater mInflater = LayoutInflater.from(this);  
		for(int i=0;i<tabTitles[category].length;i++){
			RadioButton rb = (RadioButton) mInflater.inflate(R.layout.nav_radiogroup_item, null);
			rb.setId(i);
			rb.setText(tabTitles[category][i]);
			rb.setLayoutParams(new LayoutParams(indicatorWidth,
					LayoutParams.MATCH_PARENT));
			rg_nav_content.addView(rb);
		}
		mAdapter = new TabFragmentPagerAdapter(getActivity().getSupportFragmentManager());
		mViewPager.setAdapter(mAdapter);
	}
	private void setListener() {
		ImageView iv_back = (ImageView) mView.findViewById(R.id.iv_back);
		iv_back.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
			getActivity().finish();
			}
		});
		
		 
		mViewPager.setOnPageChangeListener(new OnPageChangeListener() {
			
			@Override
			public void onPageSelected(int position) {
				if(rg_nav_content!=null && rg_nav_content.getChildCount()>position){
					((RadioButton)rg_nav_content.getChildAt(position)).performClick();
					mAdapter.notifyDataSetChanged();
				}
			}
			
			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				
			}
			
			@Override
			public void onPageScrollStateChanged(int arg0) {
				
			}
		});
		
		rg_nav_content.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				
				if(rg_nav_content.getChildAt(checkedId)!=null){
					
					TranslateAnimation animation = new TranslateAnimation(
							currentIndicatorLeft ,
							((RadioButton) rg_nav_content.getChildAt(checkedId)).getLeft(), 0f, 0f);
					animation.setInterpolator(new LinearInterpolator());
					animation.setDuration(100);
					animation.setFillAfter(true);
					
					//执行位移动画
					iv_nav_indicator.startAnimation(animation);
					
					mViewPager.setCurrentItem(checkedId);	//ViewPager 跟随�?�� 切换
					
					//记录当前 下标的距�?��侧的 距离
					currentIndicatorLeft = ((RadioButton) rg_nav_content.getChildAt(checkedId)).getLeft();
					
					mHsv.smoothScrollTo(
							(checkedId > 1 ? ((RadioButton) rg_nav_content.getChildAt(checkedId)).getLeft() : 0) - ((RadioButton) rg_nav_content.getChildAt(2)).getLeft(), 0);
				}
			}
		});
	}
	private Handler mHandler = new Handler(){
		
		@Override
		public void handleMessage(Message msg) {
			
			super.handleMessage(msg);
			switch (msg.what) {
			case 1:
				mAdapter = new TabFragmentPagerAdapter(getActivity().getSupportFragmentManager());
				mViewPager.setAdapter(mAdapter);
				break;  
			default:
				break;
			}
		}
		
	};

	Fragment ft = null;
	private View mView;
	public class TabFragmentPagerAdapter extends FragmentPagerAdapter{


		public TabFragmentPagerAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public Fragment getItem(int arg0) {
			if(arg0==0){
				ft = new LaunchUIFragment2();
				Bundle bundle=new Bundle();
				bundle.putInt("index", 0);
				ft.setArguments(bundle);
			}else
				ft = new LaunchUIFragment();
				Bundle args = new Bundle();
//				args.putString(ARGUMENTS_NAME, tabTitles[category][arg0]);
				args.putInt("index", arg0);
				args.putInt("category", category);
				ft.setArguments(args);
			return ft;
		}

		@Override
		public int getCount() {
			
			return tabTitles[category].length;
		}
	}

}
